package com.dinner.util;

import com.dinner.exception.SellerRegistrationException;

public final class RegistrationValidatorImpl implements IRegistrationValidator {

	private String sellerIDRegex = null;
	private int minSellerIDLength;
	private int maxSellerIDLength;
	private String sellerPasswordRegex = null;
	private int minSellerPasswordLength;
	private int maxSellerPasswordLength;
	private String emailRegex = null;
	/**
	 * @return the sellerIDRegex
	 */
	public String getSellerIDRegex() {
		return sellerIDRegex;
	}
	/**
	 * @param sellerIDRegex the sellerIDRegex to set
	 */
	public void setSellerIDRegex(String sellerIDRegex) {
		this.sellerIDRegex = sellerIDRegex;
	}
	/**
	 * @return the minSellerIDLength
	 */
	public int getMinSellerIDLength() {
		return minSellerIDLength;
	}
	/**
	 * @param minSellerIDLength the minSellerIDLength to set
	 */
	public void setMinSellerIDLength(int minSellerIDLength) {
		this.minSellerIDLength = minSellerIDLength;
	}
	/**
	 * @return the maxSellerIDLength
	 */
	public int getMaxSellerIDLength() {
		return maxSellerIDLength;
	}
	/**
	 * @param maxSellerIDLength the maxSellerIDLength to set
	 */
	public void setMaxSellerIDLength(int maxSellerIDLength) {
		this.maxSellerIDLength = maxSellerIDLength;
	}
	/**
	 * @return the sellerPasswordRegex
	 */
	public String getSellerPasswordRegex() {
		return sellerPasswordRegex;
	}
	/**
	 * @param sellerPasswordRegex the sellerPasswordRegex to set
	 */
	public void setSellerPasswordRegex(String sellerPasswordRegex) {
		this.sellerPasswordRegex = sellerPasswordRegex;
	}
	/**
	 * @return the minSellerPassword
	 */
	public int getMinSellerPasswordLength() {
		return minSellerPasswordLength;
	}
	/**
	 * @param minSellerPassword the minSellerPassword to set
	 */
	public void setMinSellerPasswordLength(int minSellerPasswordLength) {
		this.minSellerPasswordLength = minSellerPasswordLength;
	}
	/**
	 * @return the maxSellerPassword
	 */
	public int getMaxSellerPasswordLength() {
		return maxSellerPasswordLength;
	}
	/**
	 * @param maxSellerPassword the maxSellerPassword to set
	 */
	public void setMaxSellerPasswordLength(int maxSellerPasswordLength) {
		this.maxSellerPasswordLength = maxSellerPasswordLength;
	}
	/**
	 * @return the emailRegex
	 */
	public String getEmailRegex() {
		return emailRegex;
	}
	/**
	 * @param emailRegex the emailRegex to set
	 */
	public void setEmailRegex(String emailRegex) {
		this.emailRegex = emailRegex;
	}
	
	@Override
	public String validateBusinessTime(String openTime, String closeTime) throws SellerRegistrationException {
		java.text.DecimalFormat format = new java.text.DecimalFormat("00");
		try {
			int timeOpen = Integer.parseInt(openTime), timeClose = Integer.parseInt(closeTime);
			if (timeOpen < 0 || timeOpen > 24 || 
					timeClose < 0 || timeClose > 24 || timeOpen >= timeClose) {
				throw new SellerRegistrationException("Business hours incorrect.");
			}
			return format.format(timeOpen) + format.format(timeClose);
		} catch (NumberFormatException nfe) {
			throw new SellerRegistrationException("Business hours incorrect.");
		}
	}
	
	@Override
	public void validateRegistrationEmail(String email) throws SellerRegistrationException {
		if (email.matches(emailRegex) == false) {
			throw new SellerRegistrationException("Email format incorrect.");
		}
	}
	
	@Override
	public void validateRegistrationID(String sellerID) throws SellerRegistrationException {
		if (sellerID.matches(sellerIDRegex) == false) {
			throw new SellerRegistrationException("Seller ID format incorrect.");
		}
		if (sellerID.length() < minSellerIDLength || 
										sellerID.length() > maxSellerIDLength) {
			throw new SellerRegistrationException("Password length incorrect.");
		}
	}
	
	@Override
	public void validateRegistrationPassword(String password1, String password2) 
	 				throws SellerRegistrationException{
		if (password1.equals(password2) == false) {
			throw new SellerRegistrationException("Two passwords not consistent.");
		}
		if (password1.matches(sellerPasswordRegex) == false) {
			throw new SellerRegistrationException("Password format incorrect.");
		}
		if (password1.length() < minSellerPasswordLength || 
								password1.length() > maxSellerPasswordLength) {
			throw new SellerRegistrationException("Password length incorrect.");
		}
	}
	
	
}
